class_name Area2DDetectComponent
extends BaseComponent

@export var detectors:Array[Area2DDetector] = []
var areas:Array[Area2D] = []
var detector_dict:Dictionary = {}

func init_component(entity):
	super.init_component(entity)
	areas.clear()
	for child in get_children():
		if child is Area2D:
			child.collision_layer = 0
			areas.append(child)
	for detector in detectors:
		detector_dict[detector.detector_id] = detector
		detector.init_detector(self)
func get_area2d(index:int) -> Area2D:
	if index < areas.size():
		return areas[index]
	return null
func get_detector(detector_id:int):
	return detector_dict.get(detector_id)
func switch_area2d_active(index:int,is_active:bool):
	if index < areas.size():
		areas[index].monitoring = is_active
		areas[index].monitorable = is_active

func _init() -> void:
	component_name = "Area2DDetectComponent"
func _process(delta: float) -> void:
	if not inited:
		return
	for detector in detectors:
		if detector.active and detector.timing == GameEnum.Area2DDetectorTiming.Stay and detector.on_detect_target.get_connections().size()>0:
			detector.detect_update()
